package cn.com.oyat.idea.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        System.out.println("拦截路径: " + requestURI);
        // 获取 session
        HttpSession session = request.getSession();

        // 检查用户是否登录
        Object user = session.getAttribute("user");
        if (user == null) {
            // 未登录，重定向到登录页面
            response.sendRedirect("/login");
            return false; // 拦截请求
        }

        return true; // 放行请求
    }
}
